package com.bocai.freeman.view.activitys;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.freeman.R;
import com.bocai.freeman.base.BaseActivity;
import com.bocai.freeman.model.bean.BaseBean;
import com.bocai.freeman.presenter.UserPresenter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by admin on 2016/5/17.
 */
public class RegisterActivity extends BaseActivity {

    @Bind(R.id.et_phone)
    EditText mEtPhone;
    @Bind(R.id.et_vali_code)
    EditText mEtValiCode;
    @Bind(R.id.et_pwd)
    EditText mEtPwd;
    @Bind(R.id.btn_commit)
    Button mBtnCommit;
    @Bind(R.id.cb_protocol)
    CheckBox mCbProtocol;
    @Bind(R.id.tv_protocol)
    TextView mTvProtocol;

    @Bind(R.id.tv_obtain_code)
    TextView tvObtainCode;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;

    private UserPresenter mUserPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        mUserPresenter = new UserPresenter(this, this);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        initEvent();
    }

    @Override
    protected void initEvent() {
        mToolbar.setNavigationIcon(R.mipmap.left_return);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            getWindow()
                    .getDecorView()
                    .setSystemUiVisibility(
                            View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        click(tvObtainCode).subscribe(o -> codeRequest());
        click(mBtnCommit).subscribe(o -> registerRequest());
    }

    private void registerRequest() {
        String tel = mEtPhone.getText().toString();
        String pwd = mEtPwd.getText().toString();
        String code = mEtValiCode.getText().toString();
        mUserPresenter.register(tel, pwd, code, mCbProtocol.isChecked());
    }

    private void codeRequest() {
        mUserPresenter.obtainCode(mEtPhone.getText().toString(), "0");
    }

    @Override
    public void setData(Object obj) {
        if (obj instanceof BaseBean) {
            BaseBean bean = (BaseBean) obj;
            showToast("发送成功");
            timer.start();
            tvObtainCode.setClickable(false);
            mEtValiCode.setText(bean.getContent());
        } else if (obj instanceof String) {
            String result = (String) obj;
            showToast(result);

        }
    }

    CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            if (tvObtainCode != null) {
                tvObtainCode.setText(millisUntilFinished / 1000 + "（s）");
            }
        }

        @Override
        public void onFinish() {
            if (tvObtainCode != null) {
                tvObtainCode.setClickable(true);
                tvObtainCode.setText("再次获取");
            }
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
